<template>
	<view>
		<input class="width-a100" type="text" v-model="val" />
		<view>临时语音识别结果：{{recognizing}}</view>
		<view>最终语音识别：{{recognition}}</view>
		<view>结束语音识别：{{end}}</view>
		<view>语音识别错误：{{error}}</view>
		<button type="primary" class="primary" @tap="startSpeech">开始识别</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				val: '',
				recognizing: '',
				recognition: '',
				end: '',
				error: ''
			};
		},
		methods: {
			startSpeech(){
				
				let text_a = '';
				let _this = this;
				
				plus.speech.addEventListener("recognizing", function(e){
					text_a += e.result;
					_this.recognizing = text_a;
					// uni.showToast({icon: 'none', title: "语音中："+text_a});
				}, false);
				
				plus.speech.addEventListener("recognition", function(e){
					text_a += e.result;
					_this.recognition = text_a;
					// uni.showToast({icon: 'none', title: "语音中："+text_a});
				}, false);
				
				plus.speech.addEventListener("end", function(){
					// uni.showToast({icon: 'none', title: "语音结束："+text_a});
					_this.end = text_a;
				}, false);
				
				plus.speech.addEventListener("error", function(e){
					// uni.showToast({icon: 'none', title: "语音结束："+text_a});
					_this.error = e.message;
				}, false);
				
				uni.showToast({icon: 'none', title: '开始语音识别：'});
				
				let options = {
					continue: true,
					engine: 'baidu'
				};
				
				this.isStartSpeech = true;
				
				let text = '';
				
				plus.speech.startRecognize(options, function(s){
					text += s;
					uni.showToast({icon: 'none', title: text});
					_this.val = text;
				}, function(e){
					uni.showToast({icon: 'none', title: "语音识别失败："+e.message});
					plus.speech.stopRecognize();
				});
				
				setTimeout(()=>{
					plus.speech.stopRecognize();
				}, 10000);
				
			}
		}
	}
</script>

<style>

</style>
